//
//  UIDevice+Extension.swift
//  AIPhotoProcess
//
//  Created by aw031 on 17/10/2025.
//

import Foundation
import UIKit
import AppTrackingTransparency
import AdSupport
import CoreTelephony

extension UIDevice {
    
    static var bundleIdentifier: String {
        return Bundle.main.infoDictionary?["CFBundleIdentifier"] as? String ?? ""
    }
    
    static var deviceUUID: String {
        guard let uuid = UIDevice.current.identifierForVendor else {
            return ""
        }
        return uuid.uuidString
    }
    
    static var idfaUUID: String {
        return ASIdentifierManager.shared().advertisingIdentifier.uuidString
    }
    
    static var preferredLanguage: String? {
        guard let languages = Locale.preferredLanguages.first else {
            return nil
        }
        let languageComponents = languages.components(separatedBy: "-")
        return languageComponents.first
    }
    
    static var deviceSystemVersion: String {
        return UIDevice.current.systemVersion
    }
    
    static var appName: String {
        return Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String ??
        Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as? String ??
        "App"
    }
    
    static var countryCode: String {
        let networkInfo = CTTelephonyNetworkInfo()
        var country: String = ""
        
        if #available(iOS 12.0, *) {
            if let carriers = networkInfo.serviceSubscriberCellularProviders {
                if let carrier = carriers.values.first {
                    country = carrier.isoCountryCode?.uppercased() ?? ""
                }
            }
        } else {
            // Fallback for earlier versions
            if let carrier = networkInfo.subscriberCellularProvider {
                country = carrier.isoCountryCode?.uppercased() ?? ""
            }
        }
        
        if country.isEmpty || country == "--" {
            country = currentCountryCode
        }
        
        return country
    }
    
    static var currentCountryCode: String {
        return Locale.current.regionCode ?? ""
    }

}
